package com.aihna.system.controller.system;

import java.util.List;

import com.aihna.common.pay.domain.bo.SysPaymentChannelBo;
import com.aihna.common.pay.domain.vo.SysPaymentChannelVo;
import com.aihna.common.pay.service.ISysPaymentChannelService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.aihna.common.idempotent.annotation.RepeatSubmit;
import com.aihna.common.log.annotation.Log;
import com.aihna.common.web.core.BaseController;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.common.core.domain.R;
import com.aihna.common.core.validate.AddGroup;
import com.aihna.common.core.validate.EditGroup;
import com.aihna.common.log.enums.BusinessType;
import com.aihna.common.excel.utils.ExcelUtil;
import com.aihna.common.mybatis.core.page.TableDataInfo;

/**
 * 支付渠道
 *
 * @author Joseph.Chen
 * @date 2024-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/paymentChannel")
public class SysPaymentChannelController extends BaseController {

    private final ISysPaymentChannelService sysPaymentChannelService;

    /**
     * 查询支付渠道列表
     */
    @SaCheckPermission("system:paymentChannel:list")
    @GetMapping("/list")
    public TableDataInfo<SysPaymentChannelVo> list(SysPaymentChannelBo bo, PageQuery pageQuery) {
        return sysPaymentChannelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付渠道列表
     */
    @SaCheckPermission("system:paymentChannel:export")
    @Log(title = "支付渠道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysPaymentChannelBo bo, HttpServletResponse response) {
        List<SysPaymentChannelVo> list = sysPaymentChannelService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付渠道", SysPaymentChannelVo.class, response);
    }

    /**
     * 获取支付渠道详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:paymentChannel:query")
    @GetMapping("/{id}")
    public R<SysPaymentChannelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysPaymentChannelService.queryById(id));
    }

    /**
     * 新增支付渠道
     */
    @SaCheckPermission("system:paymentChannel:add")
    @Log(title = "支付渠道", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysPaymentChannelBo bo) {
        return toAjax(sysPaymentChannelService.insertByBo(bo));
    }

    /**
     * 修改支付渠道
     */
    @SaCheckPermission("system:paymentChannel:edit")
    @Log(title = "支付渠道", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysPaymentChannelBo bo) {
        return toAjax(sysPaymentChannelService.updateByBo(bo));
    }

    /**
     * 删除支付渠道
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:paymentChannel:remove")
    @Log(title = "支付渠道", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysPaymentChannelService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取支付渠道详细信息
     *
     */
    @SaCheckPermission("system:paymentChannel:query")
    @GetMapping("/getPaymentChannelByCode")
    public R<SysPaymentChannelVo> getPaymentChannelByCode(Long appId, String code) {
        SysPaymentChannelBo bo = new SysPaymentChannelBo();
        bo.setAppId(appId);
        bo.setCode(code);
        bo.setStatus(0);
        return R.ok(sysPaymentChannelService.getPaymentChannelByCode(bo));
    }
}
